﻿using Infrastructure.DbUtility;
using Infrastructure.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

public partial class admin_recom_subject : BasePage
{
    protected string SortCol
    {
        get { return ViewState["SortCol"] == null ? string.Empty : (string)ViewState["SortCol"]; }
        set { ViewState["SortCol"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Index = 1;
            this.LoadGradeSubject(1, string.Empty);
        }
    }
    private void LoadGradeSubject(int index, string subject)
    {
        string sql = string.Empty, filter = string.Empty;
        if (!string.IsNullOrWhiteSpace(subject)) filter += " AND C.[name] LIKE  '%' + {1} + '%'";

        if (filter.Length > 0) filter = filter.Substring(4);

        sql = "SELECT COUNT(1) count FROM GradeSubject A (NOLOCK) "
                                                               + " LEFT JOIN Grade B (NOLOCK) ON A.gid = B.nid "
                                                               + " LEFT JOIN Subject C (NOLOCK) ON A.sid = C.sid " + (filter == string.Empty ? string.Empty : " WHERE " + filter) + Environment.NewLine;

        if (index == 1)
        {
            sql += "SELECT TOP " + PAGE_SIZE + " A.*,B.[name] gname,C.[name] sname,ISNULL(C.visits,0) visits,C.[recommand] FROM GradeSubject A (NOLOCK) "
                                                                                                           + " LEFT JOIN Grade B (NOLOCK) ON A.gid = B.nid "
                                                                                                           + " LEFT JOIN Subject C (NOLOCK) ON A.sid = C.sid " + (filter == string.Empty ? string.Empty : " WHERE " + filter) + SortCol;
        }
        else
        {
            sql += "SELECT TOP " + PAGE_SIZE + " A.*,B.[name] gname,C.[name] sname,ISNULL(C.visits,0) visits,C.[recommand] FROM GradeSubject (NOLOCK) "
                                                                                     + " LEFT JOIN Grade B (NOLOCK) ON A.gid = B.nid "
                                                                                     + " LEFT JOIN Subject C (NOLOCK) ON A.sid = C.sid WHERE A.gsid NOT IN (SELECT TOP " + ((index - 1) * PAGE_SIZE) + " gsid FROM GradeSubject (NOLOCK)" + SortCol + ")" + (filter == string.Empty ? string.Empty : " AND " + filter) + SortCol;
        }

        DbHelper db = new DbHelper();
        var ds = db.ExecuteQueryEx(sql, subject);

        this.Index = index;
        int count = ds.Tables[0].Rows[0].Value<int>("count");
        this.TotalSize = count / PAGE_SIZE + (count % PAGE_SIZE == 0 ? 0 : 1);

        lstGradeSubject.DataSource = ds.Tables[1];
        lstGradeSubject.DataBind();
    }
    protected void btnPost_Click(object sender, EventArgs e)
    {
        var subject = txtName.Text.Trim();
        this.Index = 1;
        this.LoadGradeSubject(1, subject);
    }
    protected void btnNavigate_Click(object sender, EventArgs e)
    {
        string sindex = Request["txtGoTo"];
        int index = 0;
        if (!int.TryParse(sindex, out index) || index < 1) return;

        var subject = txtName.Text.Trim();
        this.Index = index;

        this.LoadGradeSubject(index, subject);
    }
    protected void SortByVisits_Click(object sender, EventArgs e)
    {
        if (!SortCol.EndsWith("ASC")) SortCol = " ORDER BY visits ASC";
        else SortCol = " ORDER BY visits DESC";

        var subject = txtName.Text.Trim();
        this.LoadGradeSubject(this.Index, subject);
    }
    protected void HotAll_Click(object sender, EventArgs e)
    {
        var btn = (sender as Button);
        var keys = Request["selector"];
        if (string.IsNullOrWhiteSpace(keys)) return;
        var arr = keys.Split(',').Select<string, object>(k => int.Parse(k)).ToArray();
        var tokens = new List<string>();
        arr.Each((k, i) => tokens.Add("{" + i + "}"));

        string sql = "UPDATE [Subject] SET [recommand] = " + (btn.CommandName == "UNCHECKALL" ? "0" : "1") + " WHERE sid IN (" + string.Join(",", tokens) + ")";
        DbHelper db = new DbHelper();
        typeof(DbHelper).GetMethod("ExecuteUpdateEx", new Type[] { typeof(string), typeof(object[]) }).Invoke(db, new object[] { sql, arr });

        var subject = txtName.Text.Trim();
        this.LoadGradeSubject(this.Index, subject);
    }
}